Given a sorted array, remove the duplicates in-place and return the new length. Each element appear only once, do not allocate extra space for another array. Problem from LeetCode
In [38]:
# Input
arr = [0,0,1,2,2,3,4,5,6,6,6,7,8,9]
In [39]:
def remove_duplicates(arr):
# Check if array is empty.
if not nums:
return 0
dup = 0
for i in range(len(arr)-1):
idx = i - dup
if arr[idx + 1] == arr[idx]:
arr.remove(arr[idx])
dup += 1
return len(arr)
In [40]:
# Output
remove_duplicates(arr)
Out[40]: